import * as sqlite_opt from "@/utils/sqliteUtil.js";

const loginAccountTableName = "t_glossary";

export const requireAllLoginAccountData = () => {
	return new Promise((resolve, reject) => {
		let sqlStr = `select * from ${loginAccountTableName} WHERE cls = 1 ORDER BY ordr ASC`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireAllLoginNoData = () => {
	return new Promise((resolve, reject) => {
		let sqlStr = `select * from ${loginAccountTableName} WHERE cls = 2 ORDER BY ordr ASC`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const createLoginAccount = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item, index) => {
		valStr += inObj[item] ? ('"' + inObj[item] + '"') : null;

		if (keys.length - 1 != index)
			valStr += ","
	})

	return new Promise((resolve, reject) => {
		let sqlStr = `insert into ${loginAccountTableName} (${keyStr}) values(${valStr})`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const deleteLoginAccount = (inId) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `delete from ${loginAccountTableName} where id = ${inId}`;

		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateLoginAccount = (inId, inObj) => {
	let valStr = `txt_value = "${inObj.txt_value}" , ordr = ${inObj.ordr}`
	return new Promise((resolve, reject) => {
		let sqlStr = `update ${loginAccountTableName} set ${valStr} where id = "${inId}"`;
		console.log("updateLoginAccount")
		console.log(sqlStr)
		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}


// 修改数据
// export const updateSQL = (name, tabName, setData, setName, setVal) => {
// 	if (JSON.stringify(setData) !== '{}') {
// 		let dataKeys = Object.keys(setData)
// 		let setStr = ''
// 		dataKeys.forEach((item, index) => {
// 			console.log(setData[item])
// 			setStr += (
// 				`${item} = ${JSON.stringify(setData[item])}${dataKeys.length - 1 !== index ? "," : ""}`)
// 		})
// 		return new Promise((resolve, reject) => {
// 			plus.sqlite.executeSql({
// 				name: name,
// 				sql: `update ${tabName} set ${setStr} where ${setName} = "${setVal}"`,
// 				success(e) {
// 					resolve(e);
// 				},
// 				fail(e) {
// 					console.log(e)
// 					reject(e);
// 				}
// 			})
// 		})
// 	} else {
// 		return new Promise((resolve, reject) => {
// 			reject("错误")
// 		});
// 	}
// }
